package com.ruoyi.information.controller.back;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.information.service.back.IStatisticsBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {


    @Autowired
    private IStatisticsBackService statisticsService;


    @Autowired
    private RedisService redisService;

    /**
     * 信息统计
     *
     * @param typeName 类型名称
     * @return {@link AjaxResult}
     */
    @GetMapping("/count")
    public AjaxResult count(@RequestParam("type") String typeName) {
        return AjaxResult.success(statisticsService.statistics(typeName));
    }

    /**
     * 用户统计
     *
     * @param typeName 类型名称
     * @return {@link AjaxResult}
     */
    @GetMapping("/userCount")
    public AjaxResult userCount(@RequestParam("type") String typeName) {
        return AjaxResult.success(statisticsService.userStatistics(typeName));
    }

    /**
     * 企业用户
     *
     * @return {@link AjaxResult}
     */
    @GetMapping("/enterprise")
    public AjaxResult enterprise(@RequestParam(value = "type",defaultValue = "全")String type) {
        return AjaxResult.success(statisticsService.enterprise(type));
    }

    /**
     * 政府用户
     *
     * @return {@link AjaxResult}
     */
    @GetMapping("/government")
    public AjaxResult government(@RequestParam(value = "type",defaultValue = "全")String type) {
        return AjaxResult.success(statisticsService.government(type));
    }

    /**
     * 专辑
     *
     * @return {@link AjaxResult}
     */
    @GetMapping("/album")
    public AjaxResult album(@RequestParam(value = "type",defaultValue = "全")String type) {
        return AjaxResult.success(statisticsService.album(type));
    }

    /**
     * 服务案例
     *
     * @return {@link AjaxResult}
     */
    @GetMapping("/case")
    public AjaxResult serviceCases(@RequestParam(value = "type",defaultValue = "全")String type) {
        return AjaxResult.success(statisticsService.serviceCases(type));
    }

    /**
     * 资讯
     *
     * @return {@link AjaxResult}
     */
    @GetMapping("/information")
    public AjaxResult information(@RequestParam(value = "type",defaultValue = "全")String type) {
        return AjaxResult.success(statisticsService.information(type));
    }

    /**
     * 频道浏览分析
     *
     * @return {@link AjaxResult}
     */
    @GetMapping("/lookCount")
    public AjaxResult lookCount(@RequestParam(value = "type",defaultValue = "全")String type) {

        String time = "";

        switch (type) {
            case "月":
                time = "month";
                break;
            case "周":
                time = "week";
                break;
            case "年":
                time = "year";
                break;
        }

        /*根据类型查对应资讯*/
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 1; i < 9; i++) {
            Map<String, Object> map = new HashMap<>();
            switch (i) {
                case 1: {
//                    map.put("name", "首页");
//                    Integer size = redisService.getCacheObject("index1");
//                    if (size == null) {
//                        map.put("count", 0);
//                    }else {
//                        map.put("count", size);
//                    }
                    break;
                }
                case 2: {
                    map.put("name", "地域营商环境");
                    Integer size = redisService.getCacheObject("index2"+time);
                    if (size == null) {
                        map.put("count", 0);
                    } else {
                        map.put("count", size);
                    }
                    break;
                }
                case 3: {
                    map.put("name", "产业物理空间");
                    Integer size = redisService.getCacheObject("index3"+time);
                    if (size == null) {
                        map.put("count", 0);
                    } else {
                        map.put("count", size);
                    }
                    break;
                }
                case 4: {
                    map.put("name", "科技产业服务港");
                    Integer size = redisService.getCacheObject("index4"+time);
                    if (size == null) {
                        map.put("count", 0);
                    } else {
                        map.put("count", size);
                    }
                    break;
                }
                case 5: {
                    map.put("name", "云课堂");
                    Integer size = redisService.getCacheObject("index5"+time);
                    if (size == null) {
                        map.put("count", 0);
                    } else {
                        map.put("count", size);
                    }
                    break;
                }
                case 6: {
                    map.put("name", "服务案例");
                    Integer size = redisService.getCacheObject("index6"+time);
                    if (size == null) {
                        map.put("count", 0);
                    } else {
                        map.put("count", size);
                    }
                    break;
                }
                case 7: {
                    map.put("name", "资讯");
                    Integer size = redisService.getCacheObject("index7"+time);
                    if (size == null) {
                        map.put("count", 0);
                    } else {
                        map.put("count", size);
                    }
                    break;
                }
                case 8: {
                    map.put("name", "关于我们");
                    Integer size = redisService.getCacheObject("index8"+time);
                    if (size == null) {
                        map.put("count", 0);
                    } else {
                        map.put("count", size);
                    }
                    break;
                }
            }
            if (i != 1) {
                list.add(map);
            }

        }
        /*排序*/
        list = (ArrayList<Object>) list.stream().sorted((o1, o2) -> {
            Integer v1 = (Integer) ((Map<String, Object>) o1).get("count");
            Integer v2 = (Integer) ((Map<String, Object>) o2).get("count");
            return v2.compareTo(v1);
        }).collect(Collectors.toList());

        return AjaxResult.success(list);
    }


    /**
     * 需求
     *
     * @return {@link AjaxResult}
     */
    @GetMapping("/need")
    public AjaxResult need(@RequestParam(value = "type",defaultValue = "全")String type) {
        return AjaxResult.success(statisticsService.need(type));
    }

}
